const jwt = require('jsonwebtoken')
const { SECRET } = require('../config')

const random = len => {
  const str = 'abcdef0123456789'
  let char = str.length
  let rand = ''
  for (let i = 0; i < len; i++) {
    rand += str.charAt(Math.floor(Math.random() * char))
  }
  return rand.toUpperCase()
}

const genToken = (payload) => {
  return jwt.sign(payload, SECRET, {expiresIn: '7d'})
}

const verifyToken = token => {
  return jwt.verify(token, SECRET)
}
module.exports = {
  random,
  genToken,
  verifyToken
}